//
//  ViewController.m
//  testCollectionView
//
//  Created by zsj1992 on 17/1/6.
//  Copyright © 2017年 bjhj. All rights reserved.
//

#import "ViewController.h"
#import "CollectionViewCell.h"
#import "XWCellModel.h"
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height



@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)NSMutableArray * data;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.flowLayout.minimumLineSpacing = 10;
    
    self.flowLayout.minimumInteritemSpacing = 0;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    
}

- (NSMutableArray *)data{
    if (!_data) {
        NSMutableArray *temp = @[].mutableCopy;
        NSArray *colors = @[[UIColor redColor], [UIColor blueColor], [UIColor yellowColor], [UIColor orangeColor], [UIColor greenColor]];
        for (int i = 0; i < 5; i ++) {
            NSMutableArray *tempSection = @[].mutableCopy;
            for (int j = 0; j < arc4random() % 6 + 5; j ++) {
                NSString *str = [NSString stringWithFormat:@"%d--%d", i, j];
                XWCellModel *model = [XWCellModel new];
                model.backGroundColor = colors[i];
                model.title = str;
                [tempSection addObject:model];
            }
            [temp addObject:tempSection.copy];
        }
        _data = temp.copy;
    }
    return _data;
}




-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return self.data.count;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    NSMutableArray * sectionModel = self.data[section];
    
    return sectionModel.count;
    
    

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableArray * sectionModel = self.data[indexPath.section];

    XWCellModel * model = sectionModel[indexPath.item];
    

    cell.lblContent.text = model.title;
    cell.backgroundColor = model.backGroundColor;
    
    //给cell添加长按的手势
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
    
    [cell addGestureRecognizer:longPress];
    
    

    return cell;
    
    
}

-(void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{

    
    CollectionViewCell * cell = (CollectionViewCell *)recognizer.view;
    
    NSLog(@"长按");
    
    CGPoint point = [recognizer locationInView:self.view];
    
    NSLog(@"%f===%f",point.x,point.y);
    
    if (recognizer.state==UIGestureRecognizerStateBegan) {
        
        NSLog(@"began");
        
        recognizer.view.hidden= YES;
        
        UIImageView * snapShootView = [[UIImageView alloc]initWithImage:[self imageFromView:cell.contentView]];

        snapShootView.frame = [self.view convertRect:recognizer.view.frame fromView:recognizer.view.superview];
        
        [self.view addSubview:snapShootView];
        
    }else if (recognizer.state==UIGestureRecognizerStateChanged) {
        NSLog(@"changed");

        
        
    }
    else if(recognizer.state==UIGestureRecognizerStateEnded||recognizer.state==UIGestureRecognizerStateCancelled){
        NSLog(@"end");
        
    
    }
    
    

    
    
}



//获得屏幕图像
- (UIImage *)imageFromView: (UIView *) theView
{
    
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}


@end
